package com.gaohe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.common.BusinessException;
import com.gaohe.common.Result;
import com.gaohe.mapper.NoticeDetailMapper;
import com.gaohe.mapper.NoticeMapper;
import com.gaohe.pojo.Notice;
import com.gaohe.pojo.NoticeDetail;
import com.gaohe.pojo.VO.NoticeVo;
import com.gaohe.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 通知表 服务实现类
 * </p>
 *
 * @author xiaoshuai
 * @since 2025-10-11
 */
@Slf4j
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private NoticeDetailMapper noticeDetailMapper;
    //mp  查两个表 -- 分为两次单表查询
    //mybatis xml sql 两表联查 单独封装结果集
    @Override
    public Result getNoticeAndDetailByNoticeId(Long noticeId) {
        //notice
        Notice notice = noticeMapper.selectById(noticeId);
        NoticeVo noticeVo = new NoticeVo();
//        noticeVo.setTitle(notice.getTitle());
        //对象拷贝 复制 同名的属性
        BeanUtils.copyProperties(notice,noticeVo);
        //noticeDetail
        LambdaQueryWrapper<NoticeDetail> qw = new LambdaQueryWrapper<>();
        qw.eq(NoticeDetail::getNoticeId,noticeId);
        NoticeDetail noticeDetail = noticeDetailMapper.selectOne(qw);
        noticeVo.setNoticeDetail(noticeDetail);
        return Result.success(noticeVo);
    }

    @Override
    @Transactional
    public Result modifyNoticeAndDetailByNoticeId(NoticeVo noticeVo) {
        try {
            //修改  notice
            noticeMapper.updateById(noticeVo);
//            int i = 1/0;
            //修改 noticeDetail
            LambdaQueryWrapper<NoticeDetail> qw = new LambdaQueryWrapper<>();
            qw.eq(NoticeDetail::getNoticeId,noticeVo.getNoticeId());
            noticeDetailMapper.update(noticeVo.getNoticeDetail(), qw);
            return Result.success();
        } catch (Exception e) {
            //事务失效  1.方法不是 public 2.runtimeException 3.trc-catch  不抛出异常
            //抛出异常  自定义异常
            throw new BusinessException("服务器繁忙，请稍后重试");
        }
//        return Result.success();
    }

    @Override
    @Transactional
    public Result deleteNoticeAndDetailByNoticeId(Long noticeId) {
        try {
            noticeMapper.deleteById(noticeId);
            int i = 1/0;
            LambdaQueryWrapper<NoticeDetail> qw = new LambdaQueryWrapper<>();
            qw.eq(NoticeDetail::getNoticeId,noticeId);
            noticeDetailMapper.delete(qw);
            return Result.success();
        } catch (Exception e) {
            log.info("NoticeServiceImpl::deleteNoticeAndDetailByNoticeId,异常信息为：{}",e.getMessage());
            throw new BusinessException("网络连接不佳，请稍后重试");
        }
    }
}
